package com.yoomiito.app.ui.fragment.cicle;

import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yoomiito.app.R;
import com.yoomiito.app.adapter.cicle.CicleGoodRemmendAdapter_1;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.model.cicle.CicleRecommendInfo_1;
import com.yoomiito.app.model.cicle.CircleReommendGoodsData;
import com.yoomiito.app.present.cicle.CicleGoodRecommendFmPresenter_1;
import com.yoomiito.app.ui.fragment.my.MyOrderBaseFragment;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.LoginUtils;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.RouterUtils;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.utils.YMToast;
import com.yoomiito.app.widget.SpacesItemDecoration;
import com.yoomiito.app.widget.dialog.VIPTipDialog;

import java.util.List;

import cn.droidlover.xdroidmvp.net.ApiException;

/**
 * 圈子----商品推荐
 */
public class CircleGoodRecommendFragment extends MyOrderBaseFragment<CicleGoodRecommendFmPresenter_1> {
    //    @BindView(R.id.rv_order)
//    RecyclerView mRecyclerView;
    private CicleGoodRemmendAdapter_1 mAdapter;
    private VIPTipDialog dialog;

    @Override
    public CicleGoodRecommendFmPresenter_1 newP() {
        return new CicleGoodRecommendFmPresenter_1(App.mAppConfig);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        SmartRefreshLayout.LayoutParams lp = (SmartRefreshLayout.LayoutParams) mRecyclerView.getLayoutParams();
        lp.topMargin = DensityUtil.dp2px(10);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        mRecyclerView.addItemDecoration(new SpacesItemDecoration(ResUtil.getSize(R.dimen.dp_10)));
        mRecyclerView.setLayoutManager(linearLayoutManager);

        mAdapter = new CicleGoodRemmendAdapter_1(null);
        mRecyclerView.setAdapter(mAdapter);
//        getNewData();
//        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
////                Intent intent = new Intent(getActivity(),MyHistoryMonthActivity.class);
////                intent.putExtra(Constant.KEY_MONTH,mList.get(position).getCreate_date_month());
////                startActivity(intent);
//                Log.i("YOM","SSSS");
//            }
//        });
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            CicleRecommendInfo_1 dataBean = mAdapter.getData().get(position);
            switch (view.getId()) {
                case R.id.item_goods_3:
                    if (SpannableUtils.isEmptyOr0(dataBean.getPrice())) {
                        YMToast.showText("商品已下架");
                        return;
                    }
                    //跳转商品详情
                    RouterUtils.toGoodsDetail((BaseActivity) context, String.valueOf(dataBean.getProduct_id()), AllCode.TaoBao);
                    break;
                case R.id.tv_share:
//                    if (UserInfoConfig.getRoleId() >= LevelType.VIP_4) {
                    if (App.mAppConfig.getIsLogin()){
                        RouterUtils.toShareGoods((BaseActivity) context, String.valueOf(dataBean.getProduct_id()), AllCode.TaoBao);
                    }else {
//                        AllUtils.toLogin(context,AllCode.LoginIn);
                        LoginUtils.INSTANCE.toOneKeyLogin((BaseActivity)context);
                    }
//                        Router.newIntent(context)
//                                .to(GoodsShareActivity.class)
//                                .putString(Constant.ID, String.valueOf(dataBean.getProduct_id()))
//                                .putInt(Constant.TYPE, AllCode.TaoBao)
//                                .launch();
//                    } else {
//                        if (dialog == null) {
//                            dialog = new VIPTipDialog(context);
//                            dialog.setOnClickListener(v -> {
//                                dialog.dismiss();
//                                ((MainActivity) context).switchFm(1);
//                            });
//                        }
//                        dialog.show();
//                    }
                    break;
                case R.id.iv_photo://
//                    if (TextUtils.isEmpty(dataBean.getPrice()) ||
//                            "0".equals(dataBean.getPrice()) ||
//                            "0.0".equals(dataBean.getPrice()) ||
//                            "0.00".equals(dataBean.getPrice())) {
//                        YMToast.showText("商品已下架");
//                        return;
//                    }
//                    if (SpannableUtils.isEmptyOr0(dataBean.getPrice())){
//                        YMToast.showText("商品已下架");
//                        return;
//                    }
//                    //跳转商品详情
//                    RouterUtils.toGoodsDetail((BaseActivity) context, String.valueOf(dataBean.getProduct_id()), AllCode.TaoBao);

                    break;
            }

        });
    }

    @Override
    public void handleError(ApiException e) {

    }

    public void setGoodRecommendData(CircleReommendGoodsData datas) {
        List<CicleRecommendInfo_1> data = datas.getData();
        if (currentPage == 1) {
            if (data != null && data != null) {
                mAdapter.setNewData(data);
                mSmartRefreshLayout.finishRefresh(true);
                mStatusManager.showContent();
            } else {
                mStatusManager.showEmpty();
            }
        } else {
            mSmartRefreshLayout.finishLoadMore(true);
            if (data == null || data.size() == 0) {
                mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                mAdapter.addData(data);
            }
        }
        if (datas.getMin_id()!=0){
            minId = datas.getMin_id();
        }
    }



    public void finishRefresh() {
        mSmartRefreshLayout.finishRefresh(true);
        mSmartRefreshLayout.finishLoadMore(true);
    }

    @Override
    protected boolean useMyItemDecoration() {
        return true;
    }

    @Override
    protected void getNewData() {
        getP().getGoodRecommentData(minId);
    }

    @Override
    public String getEmptyContent() {
        return "无商品推荐";
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

//        if (resultCode == CiclePublishActivity.CODE_PUBLISH_CICLE) {
//            currentPage = 1;
//            getNewData();
//        }
    }
}
